package com.lxx.wiki.controller;

import com.lxx.wiki.domain.req.CategoryQueryReq;
import com.lxx.wiki.domain.req.CategorySaveReq;
import com.lxx.wiki.domain.resp.*;
import com.lxx.wiki.domain.resp.CategoryQueryResp;
import com.lxx.wiki.service.CategoryService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 查询全部
     * @return
     */
    @GetMapping("/all")
    public CommonResp all() {
        List<CategoryQueryResp> categoryQueryRespList = categoryService.all();

        CommonResp<List<CategoryQueryResp>> resp = new CommonResp<>();
        resp.setContent(categoryQueryRespList);

        return resp;
    }

    /**
     * 新增或修改category
     * @param categorySaveReq
     * @return
     */
    @PostMapping("/save")
    public CommonResp save(@Valid @RequestBody CategorySaveReq categorySaveReq) {
        categoryService.save(categorySaveReq);
        return new CommonResp<>();
    }

    /**
     * 根据id删除category
     * @param id
     * @return
     */
    @DeleteMapping("/del/{id}")
    public CommonResp del(@PathVariable Long id) {
        categoryService.del(id);
        return new CommonResp<>();
    }

}
